import store from '@/store'
import router from './router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  const token = store.getters.token
  //   console.log('888888', token)
  if (token) {
    // 如果有token
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
    // 发送请求
    // 如果vuex中已经有数据了，就不用再获取了
      if (!store.state.user.userInfo.userId) {
        const res = await store.dispatch('user/getUserInfo')
        console.log('权限控制', res)
      }
      next()
    }
  } else {
    // 如果没有token
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})
router.afterEach(() => {
  NProgress.done()
})
